package com.ddm.controller.web;

import com.ddm.entity.vxEntity.Collection;
import com.ddm.entity.vxEntity.Question;
import com.ddm.result.Result;
import com.ddm.service.vxService.CollectionService;
import com.ddm.service.vxService.QuetsionsService;
import com.ddm.utils.BeanCopyUtil;
import com.ddm.vo.vxVo.QuestionVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Web端题目收藏控制器
 */
@RestController
@RequestMapping("/collection")
public class WebCollectionController {

    @Autowired
    private CollectionService collectionService;
    
    @Autowired
    private QuetsionsService quetsionsService;

    /**
     * 收藏题目
     */
    @PostMapping("/add")
    @ApiOperation("收藏题目")
    public Result<String> addCollection(@RequestBody Collection collection) {
        if (collection.getUserId() == null || collection.getQuestionId() == null) {
            return Result.error("用户ID和题目ID不能为空");
        }
        
        try {
            // 检查是否已收藏
            boolean isCollected = collectionService.isQuestionCollected(collection.getUserId(), collection.getQuestionId());
            if (isCollected) {
                return Result.error("该题目已收藏，请勿重复添加");
            }
            
            // 保存收藏记录
            collectionService.save(collection);
            return Result.success("收藏成功");
        } catch (Exception e) {
            return Result.error("收藏失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户收藏的题目列表
     */
    @GetMapping("/questions")
    @ApiOperation("获取用户收藏的题目列表")
    public Result<List<QuestionVO>> getCollectedQuestions(@RequestParam Long userId) {
        if (userId == null) {
            return Result.error("用户ID不能为空");
        }
        
        try {
            // 获取用户收藏的题目ID列表
            List<Long> questionIds = collectionService.getCollectedQuestionIds(userId);
            
            if (questionIds.isEmpty()) {
                return Result.success(List.of());
            }
            
            // 查询题目详情
            List<Question> questions = quetsionsService.listByIds(questionIds);
            
            // 转换为VO
            List<QuestionVO> questionVOs = BeanCopyUtil.BeanCopyList(questions, QuestionVO.class);
            
            return Result.success(questionVOs);
        } catch (Exception e) {
            return Result.error("获取收藏题目列表失败：" + e.getMessage());
        }
    }

    /**
     * 取消收藏指定题目
     */
    @PostMapping("/cancel")
    @ApiOperation("取消收藏指定题目")
    public Result<String> cancelCollection(@RequestBody Collection collection) {
        if (collection.getUserId() == null || collection.getQuestionId() == null) {
            return Result.error("用户ID和题目ID不能为空");
        }
        
        try {
            // 执行取消收藏操作（Service层会先检查记录是否存在）
            boolean removed = collectionService.cancelCollection(collection.getUserId(), collection.getQuestionId());
            
            if (removed) {
                return Result.success("取消收藏成功");
            } else {
                return Result.error("取消收藏失败，该题目未收藏或已被取消");
            }
        } catch (Exception e) {
            return Result.error("取消收藏失败：" + e.getMessage());
        }
    }

    /**
     * 检查指定题目是否已收藏
     */
    @PostMapping("/check")
    @ApiOperation("检查指定题目是否已收藏")
    public Result<Map<String, Boolean>> checkCollectionStatus(@RequestBody Collection collection) {
        if (collection.getUserId() == null || collection.getQuestionId() == null) {
            return Result.error("用户ID和题目ID不能为空");
        }
        
        try {
            boolean isCollected = collectionService.isQuestionCollected(collection.getUserId(), collection.getQuestionId());
            
            Map<String, Boolean> result = new HashMap<>();
            result.put("is_collected", isCollected);
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("检查收藏状态失败：" + e.getMessage());
        }
    }
} 